﻿using UnityEngine;
using System.Collections;

public class InventoryDisplay : MonoBehaviour {
	
	private GameObject selectedObject;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.I)) {
			closeDisplay();
		}
	}
	
	void closeDisplay () {
		this.enabled = false;
		Time.timeScale = 1;
		foreach (MouseLook ml in this.GetComponentsInChildren<MouseLook>())
			ml.enabled = true;
		selectedObject = null;
	}
	
	void OnGUI () {
		Inventory inv = (Inventory)this.gameObject.GetComponent("Inventory");
		if (inv != null) {
			int ITEMSPERROW = Mathf.CeilToInt(Mathf.Sqrt((float)inv.inventorySize));
		 	int ITEMSPERCOLUMN = Mathf.CeilToInt(Mathf.Sqrt((float)inv.inventorySize)); 
			Rect itemRect = new Rect(0, 0, Screen.width, Screen.height-200);
			GUILayoutOption buttonWidth = GUILayout.Width((itemRect.width - 100)/ ITEMSPERROW);
			GUILayoutOption buttonHeight = GUILayout.Height((itemRect.height - 100) / ITEMSPERCOLUMN);
			
			bool[,] itemButtons = new bool[ITEMSPERROW, ITEMSPERCOLUMN];
			
			GUILayout.BeginArea(itemRect);
			int itemIndex = 0;
			
			for (int i = 0 ; i < ITEMSPERCOLUMN && itemIndex < inv.inventorySize; i++)
			{
				GUILayout.BeginHorizontal();
				GUILayout.FlexibleSpace();
				for (int j = 0 ; j < ITEMSPERROW && itemIndex < inv.inventorySize; j++)
				{
					if (itemIndex < inv.inventory.Count)
						itemButtons[j,i] = GUILayout.Button(((InventoryObject)inv.inventory[itemIndex].GetComponent("InventoryObject")).itemName, buttonWidth, buttonHeight);
					else
						itemButtons[j,i] = GUILayout.Button("Empty", buttonWidth, buttonHeight);
					itemIndex++;
				}
				GUILayout.FlexibleSpace();
				GUILayout.EndHorizontal();
				GUILayout.FlexibleSpace();
			}
			
			GUILayout.EndArea();
			
			//Update Selected objects
			for (int i = 0 ; i < ITEMSPERCOLUMN; i++)
				for (int j = 0 ; j < ITEMSPERROW; j++)
					if (itemButtons[j,i]) {
						int index = j + i * ITEMSPERROW;
						if (inv.inventory.Count > index)
							selectedObject = inv.inventory[index];
						else
							selectedObject = null;
					}
			
			//Make drop/exit/combine/craft/... buttons
			GUILayout.BeginArea(new Rect(0, Screen.height - 200, Screen.width, 200));
			GUILayout.FlexibleSpace();
			GUILayout.BeginHorizontal();
			GUILayout.FlexibleSpace();
			if (selectedObject != null) {
				InventoryObject iObj = (InventoryObject) selectedObject.GetComponent("InventoryObject");
				GUILayout.Label(iObj.itemName, buttonWidth);
			} else 
				GUILayout.Label("Nothing Selected", buttonWidth);
			bool exitButton = GUILayout.Button("Exit", buttonWidth);
			bool dropButton = GUILayout.Button("Drop", buttonWidth);
			bool useButton = GUILayout.Button("Use", buttonWidth);
			GUILayout.FlexibleSpace();
			GUILayout.EndHorizontal();
			GUILayout.FlexibleSpace();
			
			
			GUILayout.EndArea();
			
			//GUI logic
			if (exitButton) {
				closeDisplay();
			} else if (dropButton) {
				inv.DropItem(selectedObject);
				selectedObject = null;
			} else if (useButton) {
				bool worked = inv.UseItem(selectedObject);
				if (worked) {
					selectedObject = null;
				} else {
					//TODO: You can't use that right now message
				}
			}
		}
	}
}
